tibasicfandomcom-20200215-history
Beginner's Guide to Programming TI-BASIC/2
Lbl and Menu An important aspect of every program is allowing the user to feel like they have some control over what goes on in the program. There are a lot of ways to let users chose their own paths. But first, you need those paths to lead somewhere. Lbl Lbl (label) is a command that divides your program into different sections. While this is not very helpful on its own, you can add commands that direct the user to different labels. Usually nested loops are more often used by advanced programmers than labels. Menu( The Menu( function is used to create some pretty awesome looking menus in your programs. Better yet, it's really simple. Look on the Menu( page to see how easy it is to make a menu. Using Lbl and Menu( Together Let's make the following program: prgmWIKI :Lbl 1 :ClrHome :Menu("I AM REALLY...","COOL",2,"SMART",3,"PRETTY",4) :Lbl 2 :Output(1,1,"YOU ARE COOL :Pause :Goto 5 :Lbl 3 :Output(1,1,"YOU ARE SMART :Pause :Goto 5 :Lbl 4 :Output(1,1,"YOU ARE PRETTY :Pause :Lbl 5 This program lets the user feel as if they are in control. It lets them pick which of the given paths they want to take, and the choice they make affects what happens in the program. In the program, whichever word they choose will appear after YOU ARE on the screen. Using only Lbl, Menu(, Output(, Pause, and ClrHome commands, you have all the tools necessary to create a complete, nice looking Text Adventure. Click on the link to see a tutorial of how to create a text adventure. If, Then, Else, and Goto If If is used as a normal conditional statement would, in conjunction with Then. This means that when you place it in a program with a condition next to it (like X=5 or N P), the next line of commands will only be executed if the statement is true (unless a not( restriction is placed before it, in which case, it will only be executed if the statement is not true). Then When separated from an if command by a colon (e.x. If X:Then), will only perform the following lines of text between the Then and Else/End if the If condition is true. Else When you place an If/Then statement into a program, there are always two options: The If/Then statement is true or the If/Then statement is false. Placing multiple If/Then statements for the same condition can get confusing. Instead, you can place an Else command, which will only perform the text after it if the If/Then statement before it is false. End An end statement has to be used to tell a program where an if/then statement ends. Anything after the end statement will occur regardless of whether the If condition is true or false. Goto Goto will simply do one task: It will GoTo the indicated label (e.x. Goto 5 will send the program to label 5). Using If, Then, Else, End, and Goto Together In order to get this program to work, we need to use one command you do not know yet: randInt(. All you need to know is that it will choose a random integer between the two indicated numbers and store it into the indicated variable. You will learn more about this command later. prgmWIKI :ClrHome :randInt(1,10) X :Output(1,1,X) :Pause :If X 5:Then :Goto 1 :Else :Goto 2 :End :Lbl 1 :Output(2,1,"GREATER THAN 5 :Goto 3 :Lbl 2 :Output(2,1,"LESS THAN/EQUAL TO 5 :Goto 3 :Lbl 3 :Output(4,1,"END This program will choose a random number between 1 and 10. It will then show you the number, and pause. If the value of the number is greater than 5, the program goes to label 1, and outputs GREATER THAN 5. If is is not, the program goes to label 1 and outputs LESS THAN/EQUAL TO 5 (the two spaces are put in to wrap the text to the next line). After, the program will go to label 3 and output END. Note on Using If and Then A common problem that new programmers run into is the difference between using "If/Then" commands as opposed to just "If" commands. Observe the two programs: prgmIFTHEN :ClrHome :Input "NUM",X :If X=5:Then :Output(2,1,"ENTERED 5" :Pause :ClrHome prgmIF :ClrHome :Input "NUM",X :If X=5 :Output(2,1,"ENTERED 5" :Pause :ClrHome Notice that neither program has the "End" command. For the IFTHEN program, The Output and Pause and second ClrHome command will only occur if X was 5 (The If/Then never ended). For the If program, the Output will only occur if X was 5, however the Pause and ClrHome command will occur even if X is not 5. This is because an If statement only lasts one line if Then is not used, and does not rely on an End statement to end. Input Input Input is one of the most important commands that could be put into a program. It will allow the user of the program to input data, then the data will get stored into a variable. For example, if Input "NUMBER:",X is put into a program, the program will print NUMBER:, then wait until the user enters data and presses enter. The data will be stored into the variable X. This can make programs, such as the one above, be much more enjoyable for the user. If we take the program above and substitute an Input command for the randInt( command, we get: prgmWIKI :ClrHome :Input "NUMBER 1-10:",X :Pause :If X 5:Then :Goto 1 :Else :Goto 2 :End :Lbl 1 :Output(2,1,"GREATER THAN 5 :Goto 3 :Lbl 2 :Output(2,1,"LESS THAN/EQUAL TO 5 :Goto 3 :Lbl 3 :Output(4,1,"END The part of the code that displayed X is no longer necessary, because the user will obviously know the number they chose. The program will then go through the same analysis, but with the inputted number instead of a random one. Guessing Games Using the Input command is the basis of nearly all programs that do not use Menu( or getKey (which you'll learn about later). One of the simplest programs you can make with it is a 1 or even 2 person Guessing Game. Check out the Guessing Game tutorial to see how to use the commands you know to create a game that you and your friends can enjoy. Category:Beginner's Guide